#ifndef COROUTINE_CONDITION_VAR_H
#define COROUTINE_CONDITION_VAR_H

#include "../base/Defind.hpp"
#include "Factor.hpp"
#include <atomic>
#include <functional>

namespace DBServer {
namespace coroutine {

class ConditionVar {
    friend class Factor;
public:
    NONCOPY_NONMOVE_CONSTRUCTOR(ConditionVar);
    void notify_one();

    void notify_all();

    void wait();

    void wait(std::function<bool()> condition);

private:

    ConditionVar();
    ~ConditionVar();

private:
    std::atomic_int64_t count;      // 统计在该条件变量上等待的协程数量
    int chan[2];                    // 通信管道
};

}   // coroutine
}   // DBServer

#endif // !COROUTINE_CONDITION_VAR_H